<?php
/**
 * @file simplecombo.php
 * @brief Clase para generar un combo simple
 * @author DyrTechnologies
 * @version V 1.0
 * @date Wed Oct 03 2007
 *
 *  Part of the CEFIRO dynamic web framework project
 *
 *  Copyright (C) 2007  Dyr technologies http://dyrtech.com.ve
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class Clase simplecombo
 */



/**
 * @brief Define la ruta a la hoja de estilos para el combo simple
 */

require_once(CEF_INPUT_OPT);
require_once(CEF_DOMAINS);

define("SIMPLECOMBO_CSS",FULL_URL."simplecombo/simplecombo.css");

$simplecombo_instance = 0;

class simplecombo extends input_opt {
	protected $name;
	private $table;
	private $value;
	private $option;
	private $selected;
	private $selection;
	private $domain;

	/**
	 * @brief Constructor por omision y parametrico de la clase, aprovechamos el hecho de que cada atributo tiene un valor por omision predefinido para no rehacer el constructor
	 *
	 * @param string $_name
	 * @param Array $_table
	 * @param string $_value
	 * @param string $_option
	 * @param string $_selection
	 * @param input_opt $_opt
	 * @return simplecombo La instancia de la clase
	 */
	public function simplecombo($_name = '__simplecombo', $_table = '', $_value = 'value', $_option = 'option', $_selected = -1, $_selection = '-Select an Item-', $_opt = ''){

		/**
		 * @brief $simplecombo_instance es una variable para incrementar el numero de simplecombos existentes
		 */
		global $simplecombo_instance;
		$simplecombo_instance++;

		/**
		 * @brief El nombre del widget es el tipo con el numero de la instancia que representa o el nombre pasado por omision en caso contrario
		 */

		if($_name!="__simplecombo")
		$this->name=$_name;
		else
		$this->name="simplecombo".$simplecombo_instance;

		/**
		 * @brief Se asignan los valores a los atributos de la clase combo simple segun vengan dados al llamar al constructor de la misma
		 * @brief notar que existen valores por omision para cada atributo
		 */

		$this->table=$_table;
		$this->value=$_value;
		$this->option=$_option;
		$this->selected=$_selected;
		$this->selection=$_selection;

		/**
		 * @brief Si las opciones del campo no estan ajustadas lo hacemos explicitamente una instancia de input_opt
		 */
		if($_opt=='')
		$_opt = new input_opt();

		$this->equalize($_opt);

		/**
		 * @brief Creamos el dominio de la clase
		 */
		$this->domain=new domain();
	}

	/**
	 * @brief Las funciones get_VAR devuelven las variables que tenga ajustadas la instancia: nombre (name), Elementos del Combo (table), valor(value), option(option), seleccionado(selected), valor de seleccion(selection), opciones de entrada (opt)
	 *
	 * @return string Valor de VAR
	 */

	public function get_name(){
		return $this->name;
	}

	public function get_table(){
		return $this->table;
	}

	public function get_value(){
		return $this->value;
	}

	public function get_option(){
		return $this->option;
	}

	public function get_selected(){
		return $this->selected;
	}

	public function get_selection(){
		return $this->selection;
	}

	public function get_opt(){
		return $this->get_input_opt();
	}

	/**
	 * @brief Las funciones set_VAR ajustan las variables de la instancia: nombre (name), Elementos del Combo (table), valor(value), option(option), seleccionado(selected), valor de seleccion(selection), opciones de entrada (opt)
	 */

	public function set_name($_name){
		$this->name=$_name;
	}

	public function set_table($_table){
		$this->table=$_table;
	}

	public function set_value($_value){
		$this->value=$_value;
	}

	public function set_option($_option){
		$this->option=$_option;
	}

	public function set_selected($_selected){
		$this->selected=$_selected;
	}

	public function set_selection($_selection){
		$this->selection=$_selection;
	}

	public function set_opt($_opt=''){
		if($_opt=='')
		$_opt = new input_opt();
		$this->equalize($_opt);
	}

	/**
	 * @brief Reeimplementamos las funciones del dominios... para facilidad al codificar en el nivel de interfaz
	 */

	public function set_domain($nu_domain){
		$this->domain = new domain();
		$this->domain->set_domain($nu_domain);
	}

	public function reset_domain(){
		$this->domain->reset();
	}

	public function add_domain($nu_domain){
		$this->domain->add_domain($nu_domain);
	}

	public function add_domains($nu_domains){
		$this->domain->add_domains($nu_domains);
	}

	public function remove_domain($nu_domain){
		$this->domain->remove_domain($nu_domain);
	}

	public function remove_domains($nu_domains){
		$this->domain->remove_domains($nu_domains);
	}

	public function get_domains(){
		return $this->domain->get_domains();
	}

	public function display_domain($_name){
		return $this->domain->display_domain($_name);
	}

	/**
	 * @brief Ajusta los valores de smarty de acuerdo a lo especificado por los atributos de la instancia de simplecombo
	 */
	private function smarty_simplecombo($print_domains = true){

		global $smarty;

		/**
		 * @brief Asignacion atributo por atributo a lo deseado en las variables de smarty
		 */
		$smarty->assign("name",$this->get_name());
		$smarty->assign("table",$this->get_table());
		$smarty->assign("value",$this->get_value());
		$smarty->assign("option",$this->get_option());
		$smarty->assign("selected",$this->get_selected());
		$smarty->assign("selection",$this->get_selection());
		$smarty->assign("domain",$this->get_domains());
		$smarty->assign("print_dom",$print_domains);
		$smarty->assign("__opt",$this->get_input_opt());

		/**
		 * @brief Asigna la hoja de estilo que contiene los rasgos del simplecombo a una variable
		 */
		$smarty->assign("simplecombocss",SIMPLECOMBO_CSS);
	}

	/**
	 * @brief Asigna el simplecombo a la variable $smarty para su renderizacion
	 *
	 */
	public function display_simplecombo($fetch = false,$print_domains = true){
		global $smarty;
		$smarty->template_dir = dirname(__FILE__).'/';
		$this->smarty_simplecombo($print_domains);
		if(!$fetch){
			$smarty->assign( $this->name , $smarty->fetch('simplecombo.tpl') );
			return 1;
		}
		else
			return $smarty->fetch('simplecombo.tpl');
	}
}

?>